<?php
$data = array();
class facebookPageAnalytic{
	
	protected $_cookie = "";
	protected $_pageId = "";
	protected $_totalFans = 0;
	protected $_results = array();
	
	public $tmpDir = "/tmp/";
	
	public function __construct($cookie = ""){
			$this->_cookie = $cookie;
	}
	
	public function WriteOut ($fn,$theData) {
		$fp = fopen($fn,"a");
		fwrite($fp,$theData."\r\n");
		fclose($fp);
	}
	
	public function getTotal()
	{
		return $this->_totalFans;
	}
	
	public function parseUnder100Fans($jsonData = "", $pageId = "", $cookie = ""){
		if($cookie == "") {
			$cookie = $this->_cookie;
		} else {
			$this->_cookie = $cookie;
		}
		$array = array();
		if($jsonData == "" && $cookie != "") {
			$url= "http://www.facebook.com/ajax/social_graph/dialog/popup.php?id={$pageId}&__a=1&__d=1";
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1");
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			$jsonData = curl_exec ($ch);
			curl_close ($ch);
		}
		if($jsonData == "" ) {
			return false;
		}
		$json = str_replace("for (;;);", "", $jsonData);
		$array = json_decode($json);
		$data = $array->onload[2];
		$start = strpos($data,"[{");
		$end = strpos($data, "}]");
		$jsonResults = substr($data,$start,$end - $start + 2);
		$array = json_decode($jsonResults);
		$this->_totalFans = $array[0]->count;
		$array = $array[0]->entities;
		return (array)$array;
	}
	
	public function parseUpper100Fans($node_id, $post_form_id, $fb_dtsg, $cookie = "", $debug = false, $tmpDir = "/var/www/nguyen/sportplugin/wp-content/plugins/stFacebookToolbox/tmp/", $useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 GTB7.1") {
		if($cookie == "") {
			$cookie = $this->_cookie;
		} else {
			$this->_cookie = $cookie;
		}
		$results = array();
		$fnRaw = $tmpDir . "data.tmp." . date("d-m-Y-H-i-s",time()) . ".txt";
		$url= "http://www.facebook.com/ajax/social_graph/fetch.php";
		$ch = curl_init();
		$stop = false;
		$page=0;
		while(!$stop)
		{
			$postFields = "page=".$page."&edge_type=fan&limit=100&class=FanManager&node_id=".$node_id."&post_form_id=".$post_form_id."&fb_dtsg=".$fb_dtsg."&post_form_id_source=AsyncRequest&__a=1";
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt ($ch, CURLOPT_POST, 1);
			curl_setopt ($ch, CURLOPT_POSTFIELDS, $postFields);
			curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			$r = curl_exec ($ch);
			$this->WriteOut($fnRaw,$r);
			if($debug) {
				echo "got page: ".$page."";
			}
			ob_flush();
			if(preg_match ('/"user_info":null/' ,$r,$matches) > 0) $stop = true;
			$page++;
		}
		curl_close ($ch);
		// decode file
		$fp = fopen($fnRaw,"r");
		while (!feof($fp))
		{
			$line = fgets($fp, filesize($fnRaw));
			$line = str_replace("for (;;);","",$line);
			$j = json_decode($line,true);
			if($j["payload"]["user_info"])
			{
				foreach($j["payload"]["user_info"] as $k => $p)
				{
					$results[$k] = $p;
				}
			}
		}
		fclose($fp);
		$this->_totalFans = count($results);
		return $results;
	}
}
?>